package org.jeecg.modules.wlhy.company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.enums.ReportPlatformEnum;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DictUtil;
import org.jeecg.modules.wlhy.company.entity.HyCompanyConfig;
import org.jeecg.modules.wlhy.company.service.IHyCompanyConfigService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.report.IReportService;
import org.jeecg.modules.wlhy.report.ReportServiceFactory;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: hy_company_config
 * @Author: jeecg-boot
 * @Date:   2021-07-01
 * @Version: V1.0
 */
@Api(tags="hy_company_config")
@RestController
@RequestMapping("/company/hyCompanyConfig")
@Slf4j
public class HyCompanyConfigController extends JeecgController<HyCompanyConfig, IHyCompanyConfigService> {
	@Autowired
	private IHyCompanyConfigService hyCompanyConfigService;

	@Autowired
	private DictUtil dictUtil;
	/**
	 * 分页列表查询
	 *
	 * @param hyCompanyConfig
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="hy_company_config-分页列表查询", notes="hy_company_config-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCompanyConfig hyCompanyConfig,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyCompanyConfig> queryWrapper = QueryGenerator.initQueryWrapper(hyCompanyConfig, req.getParameterMap());
		Page<HyCompanyConfig> page = new Page<HyCompanyConfig>(pageNo, pageSize);
		IPage<HyCompanyConfig> pageList = hyCompanyConfigService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hyCompanyConfig
	 * @return
	 */
	@AutoLog(value = "hy_company_config-添加")
	@ApiOperation(value="hy_company_config-添加", notes="hy_company_config-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HyCompanyConfig hyCompanyConfig) {
		HyCompanyConfig oldConfig = hyCompanyConfigService.getByCompanyId(hyCompanyConfig.getCompanyId());
		if(oldConfig != null) {
			return Result.error("此分公司已添加过配置信息，不可重复添加！");
		}
		HyConfig config = HyConfigUtils.getConfig();
		if(config == null){
			return Result.error("未找到参数配置信息,请联系管理员!");
		}
//		//检查油气
//		if(hyCompanyConfig.getIzGasUsed()){
//			if(!config.getGasIzUse()){
//				return Result.error("系统油气功能已关闭!");
//			}
//		}

		Integer reportPlatformType = hyCompanyConfig.getReportPlatformType();
		if(reportPlatformType == null){
			return Result.error("监管平台错误!");
		}
		if(reportPlatformType > 0){
			//在getReport中做判断 如果report==null，抛异常
			IReportService reportService = ReportServiceFactory.getReport(reportPlatformType);
			// 检查相关的上报是否开启，参数是否配置等操作
			reportService.checkReport();
		}

		if(reportPlatformType.equals(ReportPlatformEnum.REPORT_PLATFORM_XZ.getCode())){
			//产业园
			if(1 == hyCompanyConfig.getAutoReport()){
				return Result.error("产业园不支持自动上报!");
			}
		}

		if(hyCompanyConfig.getTaxUploadType() == 1){
			if(!config.getDongjiangIzUse()){
				return Result.error("天津东疆税务未配置参数!");
			}
		}

		hyCompanyConfigService.save(hyCompanyConfig);
		return Result.OK("添加成功！");
	}


	/**
	 *  编辑
	 *
	 * @param hyCompanyConfig
	 * @return
	 */
	@AutoLog(value = "hy_company_config-编辑")
	@ApiOperation(value="hy_company_config-编辑", notes="hy_company_config-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HyCompanyConfig hyCompanyConfig) {
		HyConfig config = HyConfigUtils.getConfig();
		if(config == null){
			return Result.error("未找到参数配置信息,请联系管理员!");
		}
		//检查油气
//		if(hyCompanyConfig.getIzGasUsed()){
//			if(!config.getGasIzUse()){
//				return Result.error("系统油气功能已关闭!");
//			}
//		}
		Integer reportPlatformType = hyCompanyConfig.getReportPlatformType();
		if(reportPlatformType == null){
			return Result.error("监管平台错误!");
		}

		if(reportPlatformType > 0){
			//在getReport中做判断 如果report==null，抛异常
			IReportService reportService = ReportServiceFactory.getReport(reportPlatformType);
			// 检查相关的上报是否开启，参数是否配置等操作
			reportService.checkReport();
		}

		if(reportPlatformType.equals(ReportPlatformEnum.REPORT_PLATFORM_XZ.getCode())){
			//产业园
			if(1 == hyCompanyConfig.getAutoReport()){
				return Result.error("产业园不支持自动上报!");
			}
		}

		if(hyCompanyConfig.getTaxUploadType() == 1){
			if(!config.getDongjiangIzUse()){
				return Result.error("天津东疆税务未配置参数!");
			}
		}
		hyCompanyConfigService.edit(hyCompanyConfig);
		return Result.OK("编辑成功!");
	}

//	/**
//	 *   通过id删除
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "hy_company_config-通过id删除")
//	@ApiOperation(value="hy_company_config-通过id删除", notes="hy_company_config-通过id删除")
//	@DeleteMapping(value = "/delete")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//		hyCompanyConfigService.removeById(id);
//		return Result.OK("删除成功!");
//	}
//
//	/**
//	 *  批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "hy_company_config-批量删除")
//	@ApiOperation(value="hy_company_config-批量删除", notes="hy_company_config-批量删除")
//	@DeleteMapping(value = "/deleteBatch")
//	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.hyCompanyConfigService.removeByIds(Arrays.asList(ids.split(",")));
//		return Result.OK("批量删除成功!");
//	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="hy_company_config-通过id查询", notes="hy_company_config-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyCompanyConfig hyCompanyConfig = hyCompanyConfigService.getById(id);
		if(hyCompanyConfig==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyCompanyConfig);
	}

	@ApiOperation(value="hy_company_config-通过companyId查询", notes="hy_company_config-通过companyId查询")
	@GetMapping(value = "/queryByCompanyId")
	public Result<?> queryByCompanyId(@RequestParam(name="id",required=true) String id) {
		QueryWrapper<HyCompanyConfig> queryWrapper = new QueryWrapper() ;
		queryWrapper.eq("company_id", id);
		HyCompanyConfig hyCompanyConfig = hyCompanyConfigService.getOne(queryWrapper);
		return Result.OK(hyCompanyConfig);
	}


	@GetMapping(value = "/all")
	public Result<?> getAll(HyCompanyConfig hyCompanyConfig, HttpServletRequest req) {
		QueryWrapper<HyCompanyConfig> queryWrapper = QueryGenerator.initQueryWrapper(hyCompanyConfig, req.getParameterMap());
		queryWrapper.gt("report_platform_type", 0);
		List<HyCompanyConfig> list = hyCompanyConfigService.list(queryWrapper);
		return Result.OK(dictUtil.parseArrayListDict(list));
	}


	//获取所有已经配置园区上报的分公司列表
	@GetMapping(value = "/allpark")
	public Result<?> getAllPark(HyCompanyConfig hyCompanyConfig, HttpServletRequest req) {
		QueryWrapper<HyCompanyConfig> queryWrapper = QueryGenerator.initQueryWrapper(hyCompanyConfig, req.getParameterMap());
		queryWrapper.gt("park_report_platform_type", 0);
		List<HyCompanyConfig> list = hyCompanyConfigService.list(queryWrapper);
		return Result.OK(dictUtil.parseArrayListDict(list));
	}

}
